﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using System.Reflection;
using NAttrArgs;

namespace Managistics.LoadDummyData
{
    class Program
    {
        // action to get data
        [NArg(AltName = "Method", AllowedValues = new string[] {
            "CreateItemsData",
            "CreateStockBeginingData"})]
        public string _methodArg;

        // list of parameter
        [NArg(IsConsumeRemaining = true, AltName = "Parameters")]
        private string[] _theRest;

        static void Main(string[] args)
        {
            try
            {
                new Program().Run(args);
            }
            catch (NArgException e)
            {
                Console.Error.WriteLine(e.Message);
            }
        }

        public static string InvokeStringMethod(string typeName, string methodName, string[] stringParam)
        {
            // Get the Type for the class
            Type calledType = Type.GetType("Managistics.LoadDummyData." + typeName);

            // Invoke the method itself. The string returned by the method winds up in s.
            // Note that stringParam is passed via the last parameter of InvokeMember,
            // as an array of Objects.
            String s = (String)calledType.InvokeMember(
                            methodName,
                            BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static,
                            null,
                            null,
                            new Object[] { stringParam });

            // Return the string that was returned by the called method.
            return s;
        }

        public static string InvokeStringMethod(string typeName, string methodName, int year, int month)
        {
            // Get the Type for the class
            Type calledType = Type.GetType("Managistics.LoadDummyData." + typeName);

            // Invoke the method itself. The string returned by the method winds up in s.
            // Note that stringParam is passed via the last parameter of InvokeMember,
            // as an array of Objects.
            String s = (String)calledType.InvokeMember(
                            methodName,
                            BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static,
                            null,
                            null,
                            new Object[] { year, month });

            // Return the string that was returned by the called method.
            return s;
        }

        private void Run(string[] args)
        {
            new ArgParser<Program>("LoadDummyData").Parse(this, args);

            if (_methodArg.Equals("All"))
            {
                // run all actions
            }
            else
            {
                if (_theRest.Length > 0)
                {
                    string action = _theRest[0];
                    // remove the first item in array
                    _theRest = _theRest.Where((val, idx) => idx != 0).ToArray();
                    InvokeStringMethod(_methodArg, action, _theRest);
                }
            }
        }
    }
}
